﻿
using Model;
using Model.Modules;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Xml.Linq;
using ViewModel;
using Proxy;
using Utility;

namespace SteelDetection
{
    public class Object2Int : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (Object)value;
        }
    }
    public class BoolToText : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && (bool)value)
            {
                return "是";
            }
            return "否";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (Object)value;
        }
    }

    public class GradeBackcolorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string grade = value.ToString();
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            resourceDictionary.Source = new Uri("/SteelDetection;component/Dic/BaseDictionary.xaml", UriKind.Relative); // 替换 YourAssemblyName 为你的程序集名称

            var resources = Application.Current.Resources;
            switch (grade)
            {
                case "报警":
                    return resourceDictionary["Alarm_Brush"];
                case "警告":
                    return resourceDictionary["Warning_Brush"];
                case "疑似":
                    return resourceDictionary["Suspected_Brush"];
                default:
                    return new SolidColorBrush(Colors.White);
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
    public class BatchGradeColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string grade = value == null ? "" : value.ToString();
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            resourceDictionary.Source = new Uri("/SteelDetection;component/Dic/BaseDictionary.xaml", UriKind.Relative); // 替换 YourAssemblyName 为你的程序集名称

            var resources = Application.Current.Resources;
            switch (grade)
            {
                case "较差":
                    return resourceDictionary["Alarm_Brush"];
                case "良好":
                    return resourceDictionary["Warning_Brush"];
                case "优秀":
                    return resourceDictionary["Suspected_Brush"];
                default:
                    return new SolidColorBrush(Colors.White);
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }


    public class DefectBackcolorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Defectypes detectypes = Defectypes.GetInstance();
            if (detectypes == null)
                return new SolidColorBrush(Colors.White);
            SolidColorBrush color;
            var dtype = int.Parse(value.ToString());
            var type = value.GetType();
            var dcolorstr = "#CCCCCC";
            try
            {
                dcolorstr = detectypes.Where(t => t.id == dtype).FirstOrDefault().color;
            }
            catch (Exception ex)
            {
                Logger.Info("无法识别的缺陷类型" + dtype.ToString());
            }

            return new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(dcolorstr));
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return null;
        }
    }
    public class DefectIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Defectypes detectypes = Defectypes.GetInstance();
            if (detectypes == null)
                return "";

            var dtype = int.Parse(value.ToString());
            var type = value.GetType();
            var iconstr = detectypes.Where(t => t.id == (double)value).FirstOrDefault()?.icon;

            return iconstr;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return null;
        }
    }

    public class DefectNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            Defectypes detectypes = Defectypes.GetInstance();
            if (detectypes == null)
                return "未知";
            var dtype = detectypes.Where(t => t.id == System.Convert.ToDouble(value)).FirstOrDefault();
            var typename = dtype != null ? dtype.name : "未知";
            return typename;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return null;
        }
    }
    public class DefectinfosConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var defectlist = value as ObservableCollection<image_defect>;
            if (value == null)
                return "";
            var defectinfotxt = "";
            foreach (var defect in defectlist.GroupBy(t => t.type).Select(s => new { type = s.Key, count = s.Count() }))
            {
                defectinfotxt += $"{defect.count}个{Defectypes.GetInstance().Where(t => t.id == defect.type).FirstOrDefault()?.name}; ";
            }
            return defectinfotxt;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new ObservableCollection<image_defect>();
        }
    }

    public class CameraList : List<camera>
    {
        private static CameraList instance = new CameraList();
        public static CameraList GetInstance()
        {
            return instance;
        }
        private CameraList()
        {

            foreach (var camera in StaticData.CameraList)
            {
                this.Add(camera);
            }
        }
    }



    public class Defectypes : List<defect_type>
    {
        private static Defectypes instance = new Defectypes();
        public static Defectypes GetInstance()
        {
            return instance;
        }
        private Defectypes()
        {
            //包括未检出的缺陷类型，用于转换显示
            foreach (var type in StaticData.AllDefectTypes)
            {
                this.Add(type);
            }
        }
    }

    public class ParentDefectypes : List<defect_type>
    {
        public ParentDefectypes()
        {

            foreach (var type in StaticData.DefectTypes)
            {

                this.Add(type);
            }
        }
    }

    public class NamedDefectypes : List<defect_type>
    {
        public NamedDefectypes()
        {
            foreach (var type in Defectypes.GetInstance())
            {
                this.Add(type);
            }
        }
    }

    public class SurfaceType
    {
        public int id { set; get; }
        public string name { set; get; }
    }
    public class SurfaceTypes : List<SurfaceType>
    {
        private static SurfaceTypes instance = new SurfaceTypes();

        public static SurfaceTypes GetInstance()
        {
            return instance;
        }
        private SurfaceTypes()
        {
            XDocument xdoc;
            try
            {
                xdoc = XDocument.Load(Environment.CurrentDirectory + "\\SysConfig.xml");
            }
            catch (Exception ex)
            {
                return;
            }
            var surfacelist = xdoc.Root.Element("surfacelist");
            foreach (var type in surfacelist.Elements())
            {
                this.Add(new SurfaceType()
                {
                    id = int.Parse(type.Attribute("id").Value),
                    name = type.Attribute("name").Value,
                });
            }
        }
    }

    public class ImageType
    {
        public int id { set; get; }
        public string name { set; get; }
    }
    public class ImageTypes : List<ImageType>
    {
        public ImageTypes()
        {
            XDocument xdoc;
            try
            {
                xdoc = XDocument.Load(Environment.CurrentDirectory + "\\SysConfig.xml");
            }
            catch (Exception ex)
            {

                return;
            }
            var imagetypes = xdoc.Root.Element("imagetypes");
            foreach (var type in imagetypes.Elements())
            {

                this.Add(new ImageType()
                {
                    id = int.Parse(type.Attribute("id").Value),
                    name = type.Attribute("name").Value,
                });
            }
        }
    }
    public class CameraType
    {
        public int id { set; get; }
        public string name { set; get; }
    }
    public class CameraTypes : List<camera>
    {
        private static CameraTypes instance = new CameraTypes();

        public static CameraTypes GetInstance()
        {
            return instance;
        }
        private CameraTypes()
        {
            
            foreach (var camera in StaticData.CameraList)
            {

                this.Add(new camera()
                {
                    id =camera.id,
                    name = camera.name,
                });
            }
        }
    }
    public class SurfaceTypeNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            SurfaceTypes surfaceypes = SurfaceTypes.GetInstance();
            if (surfaceypes == null)
                return "";

            var type = surfaceypes.Where(t => t.id == System.Convert.ToInt16(value)).FirstOrDefault();

            return type != null ? type.name : "";;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return null;
        }
    }

    public class CameraNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            CameraList cameras = CameraList.GetInstance();
            if (cameras == null)
                return "";

            var camera = cameras.Where(t => t.id == System.Convert.ToInt16(value)).FirstOrDefault();

            return camera != null ? camera.name : "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return null;
        }
    }

    public class UnitNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";
            var unitlistdata = StaticData.UnitList;
            if (unitlistdata == null)
                return "";

            var unit = unitlistdata.Where(t => t.id == System.Convert.ToInt16(value)).FirstOrDefault();

            return unit != null ? unit.name : "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return null;
        }
    }




    public class ImageTypeToIsCheckedCvt : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.ToString() == parameter?.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool ischecked = (bool)value;
            if (!ischecked)
                return (3 - (int)parameter).ToString();
            return parameter;

        }

    }

    public class BoolToVisiable : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var vislable = (bool)value ? Visibility.Visible : Visibility.Collapsed;
            return vislable;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }
    public class HasSelectedItemConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (int)value < 0 ? false : true;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? 0 : -1;
        }
    }
    public class HideSpicalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return int.Parse(value.ToString()) < 0 ? Visibility.Collapsed : Visibility.Visible;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (Visibility)value == Visibility.Visible ? 0 : -1;
        }
    }
    public class ExistDefectColorConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (Nullable<int>)value != null ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent);

        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == new SolidColorBrush(Colors.Transparent) ? 0 : 1;
        }
    }

    public class ExistDefectColorConvert1 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (int)value > 0 ? new SolidColorBrush(Colors.LightSeaGreen) : new SolidColorBrush(Colors.Transparent);

        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == new SolidColorBrush(Colors.Transparent) ? 0 : 1;
        }
    }

    public class HideZeroConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (int)value == 0 ? "未检出" : $"已检出{value}个";

        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (string)value == "未检出" ? 0 : 1;
        }
    }
    public class InteractModeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (bool)value ? C1.WPF.Chart.Interaction.GestureMode.XY : C1.WPF.Chart.Interaction.GestureMode.None;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (C1.WPF.Chart.Interaction.GestureMode)value == C1.WPF.Chart.Interaction.GestureMode.XY ? true : false;
        }
    }


    public class ConclusionTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DefectNameConverter converter = new DefectNameConverter();
            Defectypes defectypes = Defectypes.GetInstance();
            var conclusionText = "";
            if (value == null)
                return "";
            var ogtext = value as string;
            foreach (var item in ogtext.Split('\n'))
            {
                if (item.Length > 0)
                {
                    var typevalue = item.Split(':');
                    var df = converter.Convert(typevalue[0].Replace("-", ""), null, defectypes, null);
                    conclusionText += $"{converter.Convert(typevalue[0].Replace("-", ""), null, defectypes, null)}:{typevalue[1]}\n";
                }
            }
            if (conclusionText.Length > 0)
                conclusionText.Remove(conclusionText.Length - 2);
            return conclusionText;

        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }

    public class VisiableWhenEmptyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return value == null || string.IsNullOrEmpty(value.ToString()) ? Visibility.Visible : Visibility.Hidden;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }

    /// <summary>
    /// 当文本为空时显示参数指定的内容
    /// </summary>
    public class ShowTextWhenEmptyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return value == null || string.IsNullOrEmpty(value.ToString()) ? parameter : value;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }

    public class ConnectionStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && (bool)value == true)
                return "已连接";
            else
                return "未连接";
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return true;
        }
    }
    public class ConnectionStatusColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (bool)value ? new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString("#00ff88")) : new SolidColorBrush(Colors.Red);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }
    public class RepeatTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return value == null || string.IsNullOrEmpty(value.ToString()) ? "" : "周";
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }
    public class RepeatColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return value == null || string.IsNullOrEmpty(value.ToString()) ? new SolidColorBrush(Colors.Transparent) : new SolidColorBrush(Colors.Yellow);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }

    public class EvaluatStatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (value != null && (bool)value) ? "已评估" : "评估中";
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return false;
        }
    }
    public class EvaluatStatusColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return (value != null && (bool)value) ? new SolidColorBrush(Colors.LightGreen) : new SolidColorBrush(Colors.Yellow);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return false;
        }
    }
    public class ChartTagToChartIndex : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int tagvalue;
            if (int.TryParse(value.ToString(), out tagvalue))
            {
                return tagvalue - 1;
            }
            return 0;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }

}
